package com.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.DateUtils;
import com.common.DesUtils;
import com.common.MD5Utils;
import com.common.StringUtils;
import com.common.db.RedisUtils;
import com.common.easyui.EasyuiUtils;
import com.common.easyui.JsonTreeData;
import com.common.model.Global;
import com.common.model.UserTypeEnum;
import com.common.spring.SpringMvcUtils;
import com.dao.sys.SysManageMapper;
import com.dao.sys.SysUserMapper;
import com.dao.task.TaskMapper;
import com.model.supply.SupplyUserModel;
import com.model.sys.SysUser;

@Controller
public class SysController {
	static final Logger logger = LoggerFactory.getLogger(SysController.class);
	@Autowired
	private SysUserMapper sysUserMapper;

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/ModulePermission")
	public Map<String, String> modulePermission() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	@RequestMapping("sys/RolePermission")
	public Map<String, String> roleModulePermission() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/UserRole")
	public Map<String, String> userRole() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	@RequestMapping("sys/RoleModule")
	public Map<String, String> RoleModule() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	@RequestMapping("/sys/deptMmCodeGroupConfig")
	public Map<String, String> deptMmCodeGroupConfig() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping("sys/mmType")
	public Map<String, String> mmType() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/index")
	public Map<String, String> index() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/price")
	public Map<String, String> price() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/mm")
	public Map<String, String> mm() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/bank")
	public Map<String, String> bank() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/message")
	public Map<String, String> message() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/file")
	public Map<String, String> file() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/ExternalLog")
	public Map<String, String> EXTERNAL() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	/**
	 * url映射
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("sys/log")
	public Map<String, String> log() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		return map;
	}

	@RequestMapping("/sys/DictManage")
	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping("/sys/ModuleManage")
	public Map<String, String> ModuleManage() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping("/sys/RoleManage")
	public Map<String, String> RoleManage() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping("/sys/UserManage")
	public Map<String, String> UserManage() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping("/sys/login")
	public String login(HttpServletRequest request) {
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a8f3deaac701eda&redirect_uri=http://localhost:8060/Baseframe&response_type=code&scope=snsapi_userinfo&state=111#wechat_redirect";
//		String code=HttpClientUtil.sendGet(url, "");
//		System.out.println(code);
		return "sys/loginAMZ";
	}

	@Autowired
	RedisUtils redisUtils;

	@RequestMapping("/sys/logout")
	public String logout(HttpServletRequest request) {
		String userTypeString = (String) request.getSession().getAttribute("userType");
		if (!StringUtils.isNotNull(userTypeString)) {
			return "sys/logout";
		}
		if (userTypeString.equals(UserTypeEnum.XYUser.getValue())) {
			SysUser sysUser = SpringMvcUtils.getSysUser();
			redisUtils.del(Global.REDIS_USER_PREFIX + userTypeString + "-" + sysUser.getUserid());
		} else {
			SupplyUserModel userModel = SpringMvcUtils.getSupplyUser();
			redisUtils.del(Global.REDIS_USER_PREFIX + userTypeString + "-" + userModel.getUserId());
		}
		request.getSession().invalidate();
//		String tokenString = request.getHeader("token");
//		try {
//			redisUtils.del(DesUtils.decrypt(tokenString, DesUtils.getKey()));
//			request.getSession().invalidate();
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//		}
		return "sys/logout";
	}

	@RequestMapping("/sys/ChangePwd")
	public String ChangePwd(HttpServletRequest request) {
		return "sys/ChangePwd";
	}

	@RequestMapping("/sys/default")
	public String sysdefault(HttpServletRequest request, HttpServletResponse response) {
		return "sys/default";
	}

	@RequestMapping("/common/error")
	public String commonerror(HttpServletRequest request) {
		try {
			// System.out.println(1/0);
			return "common/error";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("errorMessage:{}", e.getMessage());
			request.setAttribute("errorMessage", "" + e.getMessage());
			return "common/error";

		}
	}

	@RequestMapping("/sys/DeptManage")
	public Map<String, String> DeptManage() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@Autowired
	private SysManageMapper sysManageMapper;

	@RequestMapping(value = "/logincas")
	public String logincas(HttpServletRequest request) {
		String token = request.getParameter("token");
		String ts = request.getParameter("ts");
		String loginid = request.getParameter("loginid");
		String salt = "cmpxyspec";
		try {
			String md5 = MD5Utils.md52(loginid + "|" + ts + "|" + salt);
			if (!md5.toUpperCase().equals(token)) {
				return "sys/loginAMZ";
			}
			long tsInteger = System.currentTimeMillis();
			if (tsInteger - Long.parseLong(ts) > 15000) {
				return "sys/loginAMZ";
			}
			String loginnameString = loginid;
			loginnameString = loginnameString == null ? "" : loginnameString;
//			String pwdString = request.getParameter("pwd");
//			pwdString = pwdString == null ? "" : pwdString;
			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("casid", loginnameString);
			SysUser sysUser = sysUserMapper.selectByCasName(hashMap);
			if (sysUser == null) {
				return "sys/loginAMZ";
			} else {
				request.getSession().setAttribute("userType", UserTypeEnum.XYUser.getValue());
				request.getSession().setAttribute("sysUser", sysUser);
				redisUtils.set(Global.REDIS_USER_PREFIX + UserTypeEnum.XYUser.getValue() + "-" + sysUser.getUserid(),
						sysUser, Global.PC_BACKGROUND_TOKEN_EXPIRE);
				String tokenString = sysUser.getUserid() + "," + System.currentTimeMillis() + ","
						+ UserTypeEnum.XYUser.getValue();
				request.setAttribute("token", DesUtils.encrypt(tokenString, DesUtils.getKey()));
				request.setAttribute("sysUser", JSONObject.toJSONString(sysUser));
				List<HashMap<String, Object>> permiHashMaps = sysManageMapper
						.selectUrlAndPermissionMap(sysUser.getUserid());
				request.setAttribute("permiHashMaps", JSONObject.toJSONString(permiHashMaps));
				return "sys/logincas";
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();

		}
	}
}
